#include "./myWinManager.h"
#include "./myApplet.h"

namespace Sky
{
	
	MsgStatus myWinMgr::handleDrawEvent( myMsgMgr* mgr,Msg* msg )
	{
		Painter* painter;

		painter = Painter::castFrom(msg->getMessage());
		xAssert(painter);
		if(painter->begin())
		{
			myWinlet* p = (myWinlet*)m_Manager.getFirstChildOf(TreeMgr::cast(this));
			while(p)
			{
				if(p->isVisible())
					p->handleSendEvent(mgr,msg);
				p =  (myWinlet*)m_Manager.getNextSiblingOf(TreeMgr::cast(p));

			}
			painter->end();
			painter->present(painter->getCanvas()->getPict());
		}
		return vContinue;
	}


	myWinlet* myWinMgr::getFirstWindow()
	{
		return (myWinlet*)m_Manager.getFirstChildOf(this);
	}

	myWinlet* myWinMgr::getLastWindow()
	{
		return (myWinlet*)m_Manager.getLastChildOf(this);
	}

	myWinlet* myWinMgr::getNextWindow( myWinlet* p )
	{
		if(TreeMgr::cast(p)->getParent() != this)
			return vNull;
		return (myWinlet*)m_Manager.getNextSiblingOf(TreeMgr::cast(p));
	}

	myWinlet* myWinMgr::getPrevWindow( myWinlet* p )
	{
		if(TreeMgr::cast(p)->getParent() != this)
			return vNull;
		return (myWinlet*)m_Manager.getPrevSiblingOf(TreeMgr::cast(p));
	}

	Void myWinMgr::notifyInvalidateRect(Rect* rect )
	{
		Painter* painter = getApplication()->getPainter();
		MessagePtr message ;
		message = skynew (myWinMsg(&myControl::handlePaint,this,painter));
		painter->setInvalidArea(rect);

		m_App->getEventMgr().handlePostEvent(message);
		return;
	}

	Void myWinMgr::notifyInvalidateControl( myControl* ctrl )
	{
		Painter* painter = getApplication()->getPainter();
		MessagePtr message ;
		message = skynew (myWinMsg(&myControl::handlePaint,this,painter));
		painter->setInvalidObject(ctrl);

		m_App->getEventMgr().handlePostEvent(message);
		return;
	}

	myWinMgr::myWinMgr():TreeNode()
	{
		m_Manager.setRoot(this);
	}

	Bool myWinMgr::attachLast( myWinlet* win )
	{
		return m_Manager.attachLastChildOf(TreeMgr::cast(this),TreeMgr::cast(win));
	}

	Bool myWinMgr::attachFirst( myWinlet* win )
	{
		return m_Manager.attachFirstChildOf(TreeMgr::cast(this),TreeMgr::cast(win));
	}

	Bool myWinMgr::attachNext( myWinlet* win,myWinlet* next )
	{
		if(TreeMgr::cast(win)->getParent() != TreeMgr::cast(this))
			return vFalse;

		return m_Manager.attachNextSiblingOf(TreeMgr::cast(win),TreeMgr::cast(next));
	}
	Bool myWinMgr::attachPrev( myWinlet* win,myWinlet* prev )
	{
		if(TreeMgr::cast(win)->getParent() != this)
			return vFalse;

		return m_Manager.attachPrevSiblingOf(TreeMgr::cast(win),TreeMgr::cast(prev));
	}

	myWinlet* myWinMgr::detachAll()
	{
		return (myWinlet*)m_Manager.detachAllChildrenOf(this);
	}

	myWinlet* myWinMgr::detachFirst( )
	{
		return (myWinlet*)m_Manager.detachFirstChildOf(this);
	}

	myWinlet* myWinMgr::detachLast( )
	{
		return (myWinlet*)m_Manager.detachLastChildOf(this);
	}

	myWinlet* myWinMgr::detach( myWinlet* win )
	{
		if(TreeMgr::cast(win)->getParent() != TreeMgr::cast(this))
			return vNull;
		return (myWinlet*)m_Manager.detach(TreeMgr::cast(win));
	}

	MsgStatus myWinMgr::handleSendEvent( myMsgMgr* mgr,Msg* event )
	{
		if(event->getEvent() == (Handler::EventId)&myWinlet::handlePaint)
			return handleDrawEvent(mgr,event);
		return vContinue;
	}
}
